package com.base.card.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.base.common.core.page.TableDataInfo;
import com.base.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.base.card.domain.TCryptCard;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.base.common.utils.StringUtils;
import com.base.card.domain.bo.TCryptCardBo;
import com.base.card.domain.vo.TCryptCardVo;
import com.base.card.mapper.TCryptCardMapper;
import com.base.card.service.ITCryptCardService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 密码卡Service业务层处理
 *
 * @author base
 * @date 2023-10-17
 */
@RequiredArgsConstructor
@Service
public class TCryptCardServiceImpl implements ITCryptCardService {

    private final TCryptCardMapper baseMapper;

    /**
     * 查询密码卡
     */
    @Override
    public TCryptCardVo queryById(String cardCode){
        return baseMapper.selectVoById(cardCode);
    }

    /**
     * 查询密码卡列表
     */
    @Override
    public TableDataInfo<TCryptCardVo> queryPageList(TCryptCardBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TCryptCard> lqw = buildQueryWrapper(bo);
        Page<TCryptCardVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询密码卡列表
     */
    @Override
    public List<TCryptCardVo> queryList(TCryptCardBo bo) {
        LambdaQueryWrapper<TCryptCard> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TCryptCard> buildQueryWrapper(TCryptCardBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TCryptCard> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getUserInfo()), TCryptCard::getUserInfo, bo.getUserInfo());
        lqw.eq(StringUtils.isNotBlank(bo.getPubkey()), TCryptCard::getPubkey, bo.getPubkey());
        lqw.eq(bo.getCardStartIdx() != null, TCryptCard::getCardStartIdx, bo.getCardStartIdx());
        lqw.eq(bo.getCardEndIdx() != null, TCryptCard::getCardEndIdx, bo.getCardEndIdx());
        lqw.eq(StringUtils.isNotBlank(bo.getOperator()), TCryptCard::getOperator, bo.getOperator());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), TCryptCard::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增密码卡
     */
    @Override
    public Boolean insertByBo(TCryptCardBo bo) {
        TCryptCard add = BeanUtil.toBean(bo, TCryptCard.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setCardCode(add.getCardCode());
        }
        return flag;
    }

    /**
     * 修改密码卡
     */
    @Override
    public Boolean updateByBo(TCryptCardBo bo) {
        TCryptCard update = BeanUtil.toBean(bo, TCryptCard.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TCryptCard entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除密码卡
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
